/**
 * @description 回文
 */

export function findPalindromeNumbers1(max: number): number[] {
	let res: number[] = []

	if (max <= 0) {
		return res
	}

	for (let i = 1; i <= max; i++) {
		const s = i.toString()
		if (s === s.split('').reverse().join('')) {
			res.push(i)
		}
	}
	return res
}

export function findPalindromeNumbers2(max: number): number[] {
	let res: number[] = []
	if (max <= 0) {
		return res
	}

	for (let i = 1; i <= max; i++) {
		const s = i.toString()
		let flag = true
		let startIndex = 0
		let endIndex = s.length - 1
		while (startIndex < endIndex) {
			if (s[startIndex] !== s[endIndex]) {
				flag = false
				break
			} else {
				startIndex++
				endIndex--
			}
		}

		if (flag) {
			res.push(i)
		}
	}

	return res
}

export function findPalindromeNumbers3(max: number): number[] {
	let res: number[] = []
	if (max <= 0) {
		return res
	}

	for (let i = 1; i <= max; i++) {
		let n = i
		let reverse = 0

		// 生成反转数, n: 123, i: 123
		while (n > 0) {
			reverse = reverse * 10 + (n % 10)
			n = Math.floor(n / 10)
		}

		// reverse: 321, n: 0

		if (i === reverse) {
			res.push(i)
		}
	}

	return res
}

// 功能测试
console.log(findPalindromeNumbers3(200))

// 性能测试
console.time('findPalindromeNumbers1')
findPalindromeNumbers1(100 * 10000)
console.timeEnd('findPalindromeNumbers1')

console.time('findPalindromeNumbers2')
findPalindromeNumbers2(100 * 10000)
console.timeEnd('findPalindromeNumbers2')

console.time('findPalindromeNumbers3')
findPalindromeNumbers3(100 * 10000)
console.timeEnd('findPalindromeNumbers3')
